<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Warranty extends Model
{
    use SoftDeletes;
    protected $table = 'warranty';
    protected $primaryKey = 'id';

    protected $fillable = ['warranty_id', 'pre_id'];

    public function dealer()
    {
        return $this->belongsTo('App\Models\Admin', 'user_id');
    }

    public function warranty_to_vehicle_coverage()
    {
        return $this->hasMany('App\Models\WarrantyToVehicleCoverage', 'warranty_id');
    }

    public function jjyy()
    {
        $model = WarrantyVerify::where('warranty_id',$this->id)
            ->where('approved', 2)
            ->orderByDesc('created_at')
            ->first();
        return @$model->note;
    }

    public function coverages()
    {
        $data = WarrantyToVehicleCoverage::where('warranty_id', $this->id)
            ->leftJoin('roll_master', 'roll_master.roll_number', 'warranty_to_vehicle_coverage.roll_number')
            ->select('warranty_to_vehicle_coverage.*', 'roll_master.product_type_id')
            ->get();
        return $data;

    }

    /**
     * 安装图片
     *
     * @return mixed
     */
    public function images()
    {
        $images = WarrantyImage::where('warranty_id', $this->id)
            ->whereNull('deleted_at')
            ->get(['id', 'image_src', 'part_id', 'warranty_id']);
        return $images;
    }
}
